<?php
namespace Action;
use HY\Action;
!defined('HY_PATH') && exit('HY_PATH not defined.');
class Set extends HYBBS {
	public function __construct(){
		parent::__construct();
		//{hook a_index_init}
		if(!IS_LOGIN){
			if(IS_AJAX && IS_POST){
				die($this->json(array('error'=>false,'info'=>'请登录后再操作')));
			}
			else{
				header("location: ". HYBBS_URLA('user','login'));
				die;
			}

		}
        //if(NOW_GID != C("ADMIN_GROUP"))


            //exit('你不是管理员!');

           

	}
	public function showSet(){
        //$rs=M("Sysinfo")->execCmd(INDEX_PATH."/FHL/FHLcontrol -lic");
        //$rsArry = explode("\n",$rs);
        //$this->json($rsArry);
      
        $date=M("Sysinfo")->execCmd("date \"+%Y-%m-%d %H:%M:%S\"");
        //$moreInfo = $rs;
        //$this->v('realFCode',$realFCode);
        //$this->v('licDate',$licDate);
        //$this->v('moreInfo',$moreInfo);
        $this->v("title2","流量控制系统");
        $this->v("title","流量控制");
        $this->v('date',$date);
		$this->view = 'CQQ';
		$this->display('set');
	}
    public function shutdown(){
        $res = M("Sysinfo")->execCmd("/home/www/shutdown");
        $this->json(["error"=>true,"info"=>"正在关机"]);
    }
    public function restart(){
        $res =M("Sysinfo")->execCmd("/home/www/reboot");
        $res = str_replace("\n","",$res);
        $this->json(["error"=>true,"info"=>"正在重启"]);
    }
    public function showUser(){
        $User = S("User");

        $pageid=intval(X('get.pageid')) or $pageid=1;
        $data = $User->select("*",array(
            "ORDER"=>['uid'=>'DESC'],
            "LIMIT" => array(($pageid-1) * $this->conf['adminuser'], $this->conf['adminuser'])
        ));

        $count = $User->count();
        $count = (!$count)?1:$count;
        $page_count = ($count % $this->conf['adminuser'] != 0)?(intval($count/$this->conf['adminuser'])+1) : intval($count/$this->conf['adminuser']);

        //{hook a_admin_user_v}
        $this->v("fj","&s=admin".EXP."user");
        $this->v("pageid",$pageid);
        $this->v("page_count",$page_count);
        $this->v('data',$data);
        $this->v('user_count',$User->count());
        $this->v('day_count',$User->count(array('atime[>]'=>strtotime(date('Y-m-d')))));

        $this->view = 'FHL';
        $this->display('user');
    }
    //附件上传
    public function uploadfile(){
	    $type=X('get.type');
	    $rootPath="";
	    $filename="";
	    if($type=="core"){
            $rootPath="FHL/";
            $filename="FengHoLn";
        }elseif ($type=="client"){
            $rootPath="upload/";
            $filename="client";
        }elseif ($type=="license"){
            $rootPath="FHL/";
            $filename="license";
        }elseif ($type=="cc"){
            $rootPath="FHL/";
            $filename="FHLcontrol";
        }else{
            $this->json(["error"=>false,"info"=>"类型错误"]);
        }



        $upload = new \Lib\Upload();
        $upload->maxSize   =     30*1024*1024 ;// 设置附件上传大小  20M
        $upload->exts      =     explode(",","fhl,zip,rar,exe");// 设置附件上传类型
        $upload->rootPath  =      INDEX_PATH. $rootPath; // 设置附件上传根目录
        $upload->replace    =   true;
        $upload->autoSub    =   false;
        $upload->saveName   =   $filename;


        $info   =   $upload->upload();
        //{hook a_post_uploadfile_4}
        //$id = 0;
        //$this->json($info);
        if($info) {
            //$this->json(["info"=>$info]);
            $File = S('File');
            $File->insert(array(
                'uid'		=>	NOW_UID,
                'filename'	=>	$info['file']['name'],
                'md5name'	=>	$info['file']['savename'],
                'filesize'	=>	$info['file']['size'],
                'atime'		=>	NOW_TIME
            ));
            $id = $File->id();
            $file_size = $info['file']['size'] / 1024; //得到kb单位
            if($file_size < 1 && $file_size > 0) //如果值为 0.x 则算作 1kb
                $file_size = 1;
            M("User")->update_int(NOW_UID,'file_size','+',$file_size);

            //{hook a_post_uploadfile_5}
            $this->json(array('error'=>true,'info'=>"上传成功",'id'=>$id,'name'=>$info['file']['savename']));
        }
        //{hook a_post_uploadfile_v}
        $this->json(array('error'=>false,"info"=>$upload->getError()));

    }

    public function showPing(){
        $tip = trim(X('get.tip'));
        if($tip == ""){
            $this->json([
                'state'=>false,
                'info'=>'ip can not empty'
            ]);
        }
        $res =M("Sysinfo")->ping($tip);
        $this->json([
            'state'=>true,
            'info'=>$res
        ]);
    }
     public function showPort(){
        $port = trim(X('get.port'));
        if($port == ""){
            $this->json([
                'state'=>false,
                'info'=>'port can not empty'
            ]);
        }
        $res =M("Sysinfo")->execCmd("/usr/bin/netstat  -nlatup |grep ".$port);;
        $this->json([
            'state'=>true,
            'info'=>explode("\n",trim($res,"\n"))
        ]);
    }
    public function showTracert(){
        $tip = trim(X('get.tip'));
        if($tip == ""){
            $this->json([
                'state'=>false,
                'info'=>'ip can not empty'
            ]);
        }
        $res =M("Sysinfo")->traceroute($tip);
        $this->json([
            'state'=>true,
            'info'=>$res
        ]);
    }
    public function showRoute(){
        $res =M("Sysinfo")->execCmd("/usr/sbin/route -n");
        $this->json([
            'state'=>true,
            'info'=>explode("\n",trim($res,"\n"))
        ]);
    }
    public function showNmcli(){
        $res =M("Sysinfo")->execCmd("/usr/bin/nmcli -a");
        $this->json([
            'state'=>true,
            'info'=>explode("\n",trim($res,"\n"))
        ]);
    }
    public function showSpeed(){
        $res =M("Sysinfo")->execCmd("/usr/bin/sar -n DEV 1 1|awk NR!=1");
        $this->json([
            'state'=>true,
            'info'=>explode("\n",trim($res,"\n"))
        ]);
    }
    public function setDate(){
        $date = trim(X('post.date'));
        $res =M("Sysinfo")->execCmd("date -s \"".$date."\"");
        $this->json([
            'state'=>true,
            'info'=>$res
        ]);
    }
    public function setPass(){
        $pass0 = trim(X('post.pass0'));
        $pass1 = trim(X('post.pass1'));
        $pass2 = trim(X('post.pass2'));
        if($pass1 != $pass2)
            $this->json([
                'state'=>false,
                'info'=>"两次密码不一致"
            ]);

        $UserLib = L("User");
        if(!$UserLib->check_pass($pass1))
            $this->json([
                'state'=>false,
                'info'=>"密码不符合规则"
            ]);
        //{hook a_user_edit_3}
        if($UserLib->md5_md5($pass0,$this->_user['salt']) != $this->_user['pass'])
            $this->json([
                'state'=>false,
                'info'=>"原始密码不正确"
            ]);
        $newpass = $UserLib->md5_md5($pass1,$this->_user['salt']);
        $this->_user['pass'] = $newpass;
        S("User")->update(array(
            'pass'=>$this->_user['pass']
        ),[
            'uid'=>NOW_UID
        ]);
        //{hook a_user_edit_4}
        cookie('HYBBS_HEX',$UserLib->set_cookie($this->_user));


        $this->json([
            'state'=>true,
            'info'=>"修改成功"
        ]);
    }
	
	
	//{hook a_index_fun}
}
